package com.jack.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jack.mapper.StudentMapper;
import com.jack.pojo.Student;
import com.jack.queryParam.StudentQueryParam;
import com.jack.queryParam.StudentViolation;
import com.jack.result.PageResult;
import com.jack.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生管理接口实现类
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    /**
     * 新增学生
     *
     * @param student
     */
    public void addStudent(Student student) {
        // 补全信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        // 添加学生
        studentMapper.addStudent(student);
    }

    /**
     * 批量删除学生
     *
     * @param ids
     */
    public void deleteStudent(ArrayList<Integer> ids) {

        studentMapper.deleteStudent(ids);
    }

    /**
     * 更新学生信息
     *
     * @param student
     */
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /**
     * 更新学生违纪信息
     *
     * @param stu
     */
    public void updateStudentScore(StudentViolation stu) {
        Student student = studentMapper.getStudentById(stu.getId());
        stu.setUpdateTime(LocalDateTime.now());
        stu.setViolationScore((short) (student.getViolationScore() + stu.getScore()));
        stu.setViolationCount((short) (student.getViolationCount()+1));

        studentMapper.updateStudentScore(stu);
    }

    /**
     * 学生分页查询
     *
     * @param studentQueryParam
     * @return
     */
    public PageResult<Student> pageStudents(StudentQueryParam studentQueryParam) {
        // 开启分页
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        // 查询数据
        List<Student> list = studentMapper.pageStudents(studentQueryParam);

        // 创建分页对象
        Page<Student> p = (Page<Student>) list;

        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 根据id查询学生信息
     *
     * @param id
     * @return
     */
    public Student getStudentById(Integer id) {
        Student stu = studentMapper.getStudentById(id);
        return stu;
    }
}
